﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class AnnexDAL
    {
        public static int Save(AnnexInfo info)
        {
            string sql = string.Format("insert into [annex]([SID],[UserID],[Name],[Path],[Size],[Flag],[Type],[CreateDate]) values({0},{1},'{2}','{3}',{4},'{5}',{6},'{7}');select @@identity;", info.SID, info.UserID, info.Name, info.Path, info.Size, info.Flag, info.Type, info.CreateDate);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));
        }
        public static int Update(AnnexInfo info)
        {
            string sql = string.Format("update [annex] set [SID]={0},[Name]='{1}',[Path]='{2}',[Size]={3},[Flag]='{4}',[Type]={5} where [ID]={6}", info.SID, info.Name, info.Path, info.Size, info.Flag, info.Type, info.ID);
            return CommonDAL.ExecuteNoneQuery(sql) ? info.ID : 0;
        }

        public static AnnexInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[annex]");
            return Simple_Data(dr);
        }
        public static List<AnnexInfo> FindListBySID(int sid, string flag)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [annex] where SID={0} and Flag='{1}'",sid, flag));
            List<AnnexInfo> list = new List<AnnexInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<AnnexInfo> FindListBySIDs(string sids, string flag)
        {
            if (string.IsNullOrEmpty(sids)) {
                sids = "0";
            }
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [annex] where SID in ({0}) and Flag='{1}'", sids, flag));
            List<AnnexInfo> list = new List<AnnexInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static bool DeleteBySID(int sid, string flag)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("delete from [annex] where SID={0} and Flag='{1}'", sid, flag));
        }
        public static List<AnnexInfo> FindPage(string keyword, int parentid, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = " and parentid="+parentid;
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[depart]", out count);
            List<AnnexInfo> list = new List<AnnexInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        private static DataTable FindPage(int startindex, int pagesize, string order, string desc, string filter, out int count)
        {
            if (order == "")
            {
                order = "ID";
            }
            if (desc == "")
            {
                desc = "desc";
            }
            SqlParameter[] sp = new SqlParameter[6];
            sp[0] = new SqlParameter("@startindex", startindex);
            sp[1] = new SqlParameter("@pagesize", pagesize);
            sp[2] = new SqlParameter("@order", order);
            sp[3] = new SqlParameter("@desc", desc);
            sp[4] = new SqlParameter("@filter", filter);
            sp[5] = new SqlParameter("@count", SqlDbType.Int, 10);
            sp[5].Direction = ParameterDirection.Output;
            DataTable dt = CommonDAL.GetDataTable("user_page", sp);
            count = DataConvert.ToInt(sp[5].Value);
            return dt;
        }
        private static AnnexInfo Simple_Data(DataRow dr)
        {
            AnnexInfo info = new AnnexInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.SID = DataConvert.ToInt(dr["SID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Path = Commons.domain + DataConvert.ToString(dr["Path"]);
                info.Img100 = Commons.GetImg(info.Path, 100);
                info.Img200 = Commons.GetImg(info.Path, 200);
                info.Img1000 = Commons.GetImg(info.Path, 1000);
                info.Size = DataConvert.ToInt(dr["Size"]);
                info.Flag = DataConvert.ToString(dr["Flag"]);
                info.Type = DataConvert.ToInt(dr["Type"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        
    }
}
